var express = require('express');
var router = express.Router();
var toquerysql = require("../db/mysql.js");

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.render('register.html');
});
router.get('/zhuce',async (req,res)=>{
  let {uname} =  req.query;
  console.log(uname)
  let sql = `select * from users where username = ${uname}`;
  let [flag,data] = await toquerysql(sql);
  console.log(flag,data)
  if(flag && data.username!=undefined)
  {
     res.send("用户名已经存在");
  }
  else
  {
    res.send("可以注册");
  }
})
router.post('/register',async (req,res)=>{
      let {tel="",pass="",pass2=""} = await req.body;
      console.log(tel);
      let sql = `select * from users where username = ${tel}`;
      let insertsql = `insert into users values(null,${tel},${pass})`;
      let [flag,data] = await toquerysql(sql);
      console.log(flag,data.value)
      if(flag && data.value!=undefined)//如果该用户存在
      {
         res.send("该用户已经存在")
      }
      else //该用户不存在
      {
        let [flag,data]  = await toquerysql(insertsql);
        if(flag)
        {
          res.send("注册成功");
        }
         
      }
})

//查询语句
module.exports = router;
